********************************************************************************
********************************************************************************
* Table 7. PROPERTY VALUES AND RENTS
********************************************************************************
********************************************************************************

use "$data/propval.dta", clear
set more off

* CREATE VARIABLES OF INTEREST

	// Area level
*gen migch_a = mig_change_all / poplag *100	// first differences
gen migch_apl = totmig / totpop13 *100	// levels

* DEFLATE VALUES	
replace propval = propval / pci_uh *100
replace rent = rent / pci_r *100

* TRIM VALUES
levelsof mpio, local(levels) 
foreach l of local levels{
bys year: egen p1 = pctile(propval) if mpio==`l', p(0.5)
bys year: egen p99 = pctile(propval) if mpio==`l', p(99.5)
gen trim = 1 if propval<=p1 | propval>=p99
replace propval = . if trim==1 & mpio==`l'
drop p1 p99 trim 
}

foreach l of local levels{
bys year: egen p1 = pctile(rent) if mpio==`l', p(0.5)
bys year: egen p99 = pctile(rent) if mpio==`l', p(99.5)
gen trim = 1 if rent<=p1 | rent>=p99
replace rent = . if trim==1 & mpio==`l'
drop p1 p99 trim 
}

* CREATE LOGS
gen lprop = log(propval)
gen lrent = log(rent)

* RESIDUALIZE PROPERTY VALUES AND RENTS
local controls type rooms i.flooring i.structure elec gas sewerage garbage water
gen lprop_res = .
levelsof year, local(levels) 
foreach i of local levels {
qui reg lprop `controls' [pw = fex12] if year==`i', cluster(mpio)
predict res_`i' if year==`i', residuals
replace lprop_res = res_`i' if year==`i'
drop res_`i'
}

local controls type i.flooring i.structure elec gas sewerage garbage water
gen lrent_res = .
levelsof year, local(levels) 
foreach i of local levels {
qui reg lrent `controls' [pw = fex12] if year==`i', cluster(mpio)
predict res_`i' if year==`i', residuals
replace lrent_res = res_`i' if year==`i'
drop res_`i'
}

******************************************************************************** 
*Estimates across areas
******************************************************************************** 

* CREATE WEIGHTS - AREA
bys area year: egen units = sum(owner * fex12)
replace units =. if year!=2013
replace units = round(units,1)
bys area: egen wprop = mode(units)
drop units

bys area year: egen units = sum(rental * fex12)
replace units =. if year!=2013
replace units = round(units,1)
bys area: egen wrent = mode(units)
drop units

/*
* CREATE MIGRANT SHARE IN 2013
gen migsh = totmig / 112162
replace migsh =. if year!=2013
bys area: egen migsh13 = mode(migsh)
drop migsh
*/

* AGREGATE DATA
collapse (mean) lprop lprop_res lrent lrent_res migch_apl IV2005_pl wprop wrent [pw=fex12], by(area year)

* DEFINE VARIABLES
global y1 	lprop_res
global y2 	lrent_res
global x 	migch_apl
global z	IV2005_pl
global w1 	wprop
global w2 	wrent


* REGRESSIONS
tsset area year

*qui reg d.$y1 $x i.year [aw = $w1], robust
qui areg $y1 $x i.year [aw = $w1], abs(area) robust
	estimates store olsa1, title (OLS)
	
*qui ivreg2 d.$y1 ($x = $z) i.year [aw = $w1], robust first small
qui ivreg2 $y1 ($x = $z) i.year i.area [aw = $w1], robust first small partial(i.area)
	estimates store iva1, title (2SLS)
	matrix list e(first)				// Store results for tables
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]	// Kleiberger-paap rk Wald F-stat
	
qui areg $y2 $x i.year [aw = $w2], abs(area) robust
	estimates store olsa2, title (OLS)
	
qui ivreg2 $y2 ($x = $z) i.year i.area [aw = $w2], robust first small partial(i.area)
	estimates store iva2, title (2SLS)
	matrix list e(first)				
	matrix first = e(first)
	estadd scalar Fstat = first[4,1]	
	
* TABULATE RESULTS
esttab olsa1 iva1 olsa2 iva2,  keep($x) ///
star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3)  ///
coeflabels($x "Immigrant inflows") ///
mgroups("Property values" "Rents", pattern(1 0 1 0)) ///
mlabels( , titles) collabels(none) obslast varwidth(20)  modelwidth(15) ///
scalars("Fstat Kleibergen-Paap F") ///
title("Panel A. Variation across areas")



	  
